package com.ohos.carbon.slice;

import com.ohos.carbon.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;
import static ohos.multimodalinput.event.TouchEvent.*;

public class ZOrderSlice extends AbilitySlice implements Component.ClickedListener {
    private Image back_button, debug, checkbox;
    private Text text_title;
    private DependentLayout stackLayout;
    private Button order, btn1, btn2, btn3, btn4;
    boolean flag = true;
    boolean tempFlag1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(Layout_ability_zorder);
        initView();
        initClick();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Z order")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Z order")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Z order", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Z order", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Z order", 1);
            pf.flushSync();
        }
    }

    private void initClick() {
        order.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                stackLayout.moveChildToFront(flag ? btn1 : btn2);
                flag = !flag;
            }
        });

        btn3.setTouchEventListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case PRIMARY_POINT_DOWN:
                    LogUtils.d("Darren", "down");
//                        stackLayout.moveChildToFront(btn1);
                    stackLayout.moveChildToFront(btn1);
//                    break;
                case POINT_MOVE:

                    break;
                case PRIMARY_POINT_UP:
                    LogUtils.d("Darren", "up");
                    stackLayout.moveChildToFront(btn2);

                    break;
            }

            return false;
        });

        btn4.setTouchEventListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case PRIMARY_POINT_DOWN:
                    LogUtils.d("Darren", "down");
                    stackLayout.moveChildToFront(btn2);
                    break;
                case POINT_MOVE:
                    LogUtils.d("Darren", "move");
                    break;
                case PRIMARY_POINT_UP:
                    LogUtils.d("Darren", "up");
                    stackLayout.moveChildToFront(btn1);
                    break;
            }
            return false;
        });
    }


    private void initView() {
        stackLayout = (DependentLayout) findComponentById(Id_layout);
        order = (Button) findComponentById(Id_order);
        btn1 = (Button) findComponentById(Id_button1);
        btn2 = (Button) findComponentById(Id_button2);
        btn3 = (Button) findComponentById(Id_button3);
        btn4 = (Button) findComponentById(Id_button4);
        back_button = (Image) findComponentById(Id_back_button);
        back_button.setClickedListener(this);
        debug = (Image) findComponentById(Id_image1);
        debug.setClickedListener(this);
        checkbox = (Image) findComponentById(Id_image2);
        checkbox.setClickedListener(this);
        text_title = (Text) findComponentById(Id_title_text);
        text_title.setText("Z order");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_carbon_uparrow));

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
            flag = true;
            stackLayout.moveChildToFront(btn2);
        } else if (component.getId() == Id_image1) {

        } else if (component.getId() == Id_image2) {

        }
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        flag = true;
        stackLayout.moveChildToFront(btn2);
    }
}
